package com.example.foodsy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.dto.SlauInfoDto;
import com.example.foodsy.entity.CompanyInfo;
import com.example.foodsy.entity.FarmInfo;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.mapper.CompanyInfoMapper;
import com.example.foodsy.mapper.FarmInfoMapper;
import com.example.foodsy.mapper.SlauInfoMapper;
import com.example.foodsy.service.SlauInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 屠宰企业产品批号信息表 服务实现类
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@Service
public class SlauInfoServiceImpl extends ServiceImpl<SlauInfoMapper, SlauInfo> implements SlauInfoService {
	@Autowired
	private CompanyInfoMapper companyInfoMapper;
	@Autowired
	private FarmInfoMapper farmInfoMapper;




	@Override
	public List<SlauInfo> listByfbId(Integer fbId) {
		List<SlauInfo> slauInfos = baseMapper.selectList(new QueryWrapper<SlauInfo>().eq("fb_id", fbId));

		return slauInfos;
	}

	@Override
	public boolean addSlauInfo(SlauInfo slauInfo) {
		slauInfo.setBatchDate(LocalDate.now());
		//设置新建状态
		slauInfo.setState(1);
		return save(slauInfo);
	}

	@Override
	public List<SlauInfoDto> listBySlauName(String slauName) {
		List<SlauInfo> slauInfos = baseMapper.selectList(new QueryWrapper<SlauInfo>().eq("slau_name", slauName));

		List<SlauInfoDto> slauInfoDtos = new ArrayList<>();
		for (SlauInfo slauInfo : slauInfos) {
			SlauInfoDto slauInfoDto = new SlauInfoDto();
			//获取企业名称
			CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", slauInfo.getSlauId()));
			//设置属性
			slauInfoDto.setCompanyName(companyInfo.getName());
			slauInfoDto.setBatchId(slauInfo.getBatchId());
			slauInfoDto.setType(slauInfo.getType());
			//获取所属企业产品批号
			FarmInfo farmInfo = farmInfoMapper.selectOne(new QueryWrapper<FarmInfo>().eq("fb_id", slauInfo.getFbId()));
			slauInfoDto.setCpId(farmInfo.getBatchId());
			slauInfoDtos.add(slauInfoDto);
		}

		return slauInfoDtos;
	}

	@Override
	public List<AddressDTO<SlauInfo>> getSlauInfoBystate(Integer state) {
		List<SlauInfo> slauInfos = baseMapper.selectList(new QueryWrapper<SlauInfo>().eq("state", state));
		List<AddressDTO<SlauInfo>> addressDTOS = new ArrayList<>();

		for (SlauInfo slauInfo : slauInfos) {
			AddressDTO<SlauInfo> addressDTO = new AddressDTO<>();
			CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", slauInfo.getSlauId()));
			addressDTO.setCompanyName(companyInfo.getName());
			addressDTO.setCompanyInfo(slauInfo);

			FarmInfo farmInfo = farmInfoMapper.selectOne(new QueryWrapper<FarmInfo>().eq("fb_id", slauInfo.getFbId()));
			addressDTO.setOriginBatchId(farmInfo.getBatchId());

			addressDTOS.add(addressDTO);
		}

		return addressDTOS;
	}

	@Override
	public AddressDTO<SlauInfo> getInfoBysbId(Integer sbId) {
		AddressDTO<SlauInfo> addressDTO = new AddressDTO<>();

		SlauInfo slauInfo = baseMapper.selectOne(new QueryWrapper<SlauInfo>().eq("sb_id", sbId));
		addressDTO.setCompanyInfo(slauInfo);
		addressDTO.setCompanyId(slauInfo.getSlauId());
		System.out.println(slauInfo);

		CompanyInfo companyInfo = companyInfoMapper.selectOne(new QueryWrapper<CompanyInfo>().eq("id", slauInfo.getSlauId()));

		addressDTO.setCityId(companyInfo.getCityId());
		addressDTO.setProvinceId(companyInfo.getProvId());



		return addressDTO;
	}


}
